package iosr11.Service;

import iosr11.data.Event;
import iosr11.data.Mark;
import iosr11.data.ProjectGroup;
import iosr11.data.Student;
import iosr11.data.Teacher;
import iosr11.gbase.dao.MarkDAO;
import iosr11.gbase.dao.StudentDAO;
import iosr11.gbase.dao.TeacherDAO;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

public class MarkServiceImpl implements MarkService {

	MarkDAO markDAO;
	StudentDAO studentDAO;
	TeacherDAO teacherDAO;
	private Logger log = Logger.getLogger("service-log");
	
	public MarkServiceImpl(){
		
	}
	
	
	
	@Override
	public Class<Mark> getServiceEntityClass() {
		return Mark.class;
	}



	@Override
	public List<Mark> getGroupMarks(Event event, ProjectGroup group) {
		// TODO Auto-generated method stub
		if (group.getStudents()==null){
			log.info("brak podanych studentow - poszukiwanie");
			
		}
		return null;
	}



	@Override
	public Mark getMark(String id) {
		log.info("pobieranie oceny po id");
		Mark mark = markDAO.getByID(id);
		mark.setStudent(studentDAO.getByID(mark.getStudent().getId()));
		mark.setTeacher(teacherDAO.getByID(mark.getTeacher().getId()));
		return mark;
	}

	@Override
	public boolean createMark(Student student, Teacher teacher, Mark mark) {
		if (student==null) {
			log.warning("nie przekazano studenta - null");
			return false;
		}
		if (teacher==null) {
			log.warning("nie przekazano nauczyciela - null");
			return false;
		}
		if (mark==null) {
			log.warning("nie przekazano oceny - null");
			return false;
		}
		mark.setTeacher(teacher);
		student.addMark(mark);
		mark.setPoints(mark.getGrade()*10);
		mark.setDate(new Date(Calendar.getInstance().getTimeInMillis()));
		
		return markDAO.add(mark);
	}

	@Override
	public boolean deleteMark(Mark mark) {
		log.info("usuwanie ocenu o id: "+mark.getId());
		return markDAO.delete(mark);
	}

	@Override
	public boolean updateMark(Mark mark) {
		log.info("aktualizacja oceny o id: "+mark.getId());
		return markDAO.update(mark);
	}
	
	// ==============================================================
	// settery dla springa

	public void setMarkDAO(MarkDAO markDAO) {
		this.markDAO = markDAO;
	}

	public void setStudentDAO(StudentDAO studentDAO) {
		this.studentDAO = studentDAO;
	}

	public void setTeacherDAO(TeacherDAO teacherDAO) {
		this.teacherDAO = teacherDAO;
	}
	
	

}
